
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[Seasons_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[Seasons_Insert]

IF OBJECT_ID(N'[dbo].[Seasons_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[Seasons_Update]

IF OBJECT_ID(N'[dbo].[Seasons_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[Seasons_Delete]

IF OBJECT_ID(N'[dbo].[Seasons_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[Seasons_Select]

--endregion

GO


--region [dbo].[Seasons_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Seasons_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdSeason int = null,
	@SeasonName nvarchar(200) = null,
	@StartDate datetime = null,
	@EndDate datetime = null,
	@IsActived bit = null,
	@IdTournament int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[Seasons]
					WHERE
					(
						(@IdSeason is null OR [Seasons].[IdSeason] = @IdSeason)
						AND (@SeasonName is null OR [Seasons].[SeasonName] like @SeasonName)
						AND (@StartDate is null OR [Seasons].[StartDate] = @StartDate)
						AND (@EndDate is null OR [Seasons].[EndDate] = @EndDate)
						AND (@IsActived is null OR [Seasons].[IsActived] = @IsActived)
						AND (@IdTournament is null OR [Seasons].[IdTournament] = @IdTournament)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[Seasons].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdSeason' and @SortType = 1 THEN [Seasons].[IdSeason] END ASC,
						CASE WHEN @SortBy = 'IdSeason' and @SortType = 0 THEN [Seasons].[IdSeason] END DESC,
						CASE WHEN @SortBy = 'SeasonName' and @SortType = 1 THEN [Seasons].[SeasonName] END ASC,
						CASE WHEN @SortBy = 'SeasonName' and @SortType = 0 THEN [Seasons].[SeasonName] END DESC,
						CASE WHEN @SortBy = 'StartDate' and @SortType = 1 THEN [Seasons].[StartDate] END ASC,
						CASE WHEN @SortBy = 'StartDate' and @SortType = 0 THEN [Seasons].[StartDate] END DESC,
						CASE WHEN @SortBy = 'EndDate' and @SortType = 1 THEN [Seasons].[EndDate] END ASC,
						CASE WHEN @SortBy = 'EndDate' and @SortType = 0 THEN [Seasons].[EndDate] END DESC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 1 THEN [Seasons].[IsActived] END ASC,
						CASE WHEN @SortBy = 'IsActived' and @SortType = 0 THEN [Seasons].[IsActived] END DESC,
						CASE WHEN @SortBy = 'IdTournament' and @SortType = 1 THEN [Seasons].[IdTournament] END ASC,
						CASE WHEN @SortBy = 'IdTournament' and @SortType = 0 THEN [Seasons].[IdTournament] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[Seasons]
			Where 
			(
				(@IdSeason is null OR [Seasons].[IdSeason] = @IdSeason)
				AND (@SeasonName is null OR [Seasons].[SeasonName] like @SeasonName)
				AND (@StartDate is null OR [Seasons].[StartDate] = @StartDate)
				AND (@EndDate is null OR [Seasons].[EndDate] = @EndDate)
				AND (@IsActived is null OR [Seasons].[IsActived] = @IsActived)
				AND (@IdTournament is null OR [Seasons].[IdTournament] = @IdTournament)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[Seasons_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[Seasons_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Seasons_Insert]
	@IdSeason int OUTPUT,
	@SeasonName nvarchar(200),
	@StartDate datetime,
	@EndDate datetime,
	@IsActived bit,
	@IdTournament int

AS


INSERT INTO [dbo].[Seasons] 
(
	[SeasonName],
	[StartDate],
	[EndDate],
	[IsActived],
	[IdTournament]
)
VALUES 
(
	@SeasonName,
	@StartDate,
	@EndDate,
	@IsActived,
	@IdTournament
)

SET @IdSeason = SCOPE_IDENTITY()

--end [dbo].[Seasons_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[Seasons_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Seasons_Update]
	@IdSeason int,
	@SeasonName nvarchar(200),
	@StartDate datetime,
	@EndDate datetime,
	@IsActived bit,
	@IdTournament int
AS


UPDATE [dbo].[Seasons] SET
	[SeasonName] = @SeasonName,
	[StartDate] = @StartDate,
	[EndDate] = @EndDate,
	[IsActived] = @IsActived,
	[IdTournament] = @IdTournament
WHERE
	[IdSeason] = @IdSeason

--end [dbo].[Seasons_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[Seasons_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[Seasons_Delete]
	@IdSeason int
AS


DELETE FROM [dbo].[Seasons]
WHERE
(
	[IdSeason] = @IdSeason
)

--end [dbo].[Seasons_Delete]
--endregion

GO
--=========================================================================================--

